package com.itheima.io1.myzipstream;

import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * @author zhangbaiyan
 * @date 2024-09-22 14:39
 * @description:压缩流
 * @version:
 */
public class test2 {
    public static void main(String[] args) throws IOException {
        File src = new File("C:\\Users\\17685\\Desktop\\简历\\绩点.png");
        File dest = new File("C:\\Users\\17685\\Desktop\\新建文件夹");
        toZip(src, dest);

    }
    public static void toZip(File src, File dest) throws IOException {
        //1.创建压缩流关联压缩包
        ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(new File(dest,"a.zip")));
                //2.创建ZipEntry对象,表示压缩包里面的每一个文件和文件夹;
        ZipEntry entry = new ZipEntry("a.txt");
        //3.把zipEntry对象放入压缩包当中
        zos.putNextEntry(entry);
        //把src文件中的数据写道压缩包中
        FileInputStream fis = new FileInputStream(src);
        int b;
        while ((b = fis.read()) != -1) {
            zos.write(b);
        }

        zos.closeEntry();
        zos.close();

    }
}
